package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.basicInformation.label.OneLabelDto;
import com.dg.cuckoo.admin.dto.basicInformation.label.TwoLabelDto;
import com.dg.cuckoo.admin.repository.OneLabelRepository;
import com.dg.cuckoo.admin.repository.TwoLableRepository;
import com.dg.cuckoo.admin.service.TwoLabelService;
import com.dg.cuckoo.admin.vo.FreeVo;
import com.dg.cuckoo.common.utils.PageModel;
import com.dg.cuckoo.core.ServiceException;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TwoLabelServiceimpl implements TwoLabelService {

    @Autowired
    private TwoLableRepository repository;
    @Autowired
    private OneLabelRepository oneLabelRepository;

    @Override
    public List<FreeVo> selectView() {
        List<TwoLabelDto> all = repository.findAll();
        List<String> list=new ArrayList<>();
        for(TwoLabelDto dto:all){
            list.add(dto.getOneLabel());
        }
        List<String> newList = list.stream().distinct().collect(Collectors.toList());
        List<FreeVo> freeVos=new ArrayList<>();
        for(String s:newList){
            freeVos.add(new FreeVo(s,repository.findTwoLabelDtosByOneLabel(s)));
        }

        return freeVos;
    }

    @Override
    public void save(TwoLabelDto dto) {
        checkSave(dto);
        repository.save(dto);
    }

    @Override
    public void delect(String id) {
        if(id==null){
            throw new ServiceException("请选择标签");
        }
        repository.deleteById(id);
    }

    @Override
    public TwoLabelDto select(String id) {
        if(id==null){
            throw new ServiceException("请选择标签");
        }
        return repository.findById(id).get();
    }

    @Override
    public List<TwoLabelDto> openSelect(String name) {
        if(name==null){
            throw new ServiceException("请选择标签");
        }
        return repository.findTwoLabelDtosByOneLabel(name);
    }

    @Override
    public PageModel page(Integer page, Integer pageSize) {
        Integer one = page;
        page = (page - 1) * pageSize;
        int size = repository.findAll().size();
        List<TwoLabelDto> list = repository.page(page, pageSize);
        return new PageModel(one,pageSize,size,list);
    }

    @Override
    public void update(String id, TwoLabelDto dto) {
        if(id==null){
            throw new ServiceException("请选择行");
        }
        dto.setId(id);
        repository.save(dto);
    }

    public void checkSave(TwoLabelDto dto){
        if(dto.getSerialNumber()==null){
            throw new ServiceException("序号不能为空");
        }
        if(dto.getOneLabel()==null){
            throw new ServiceException("一级标签名称不能为空");
        }
        if(dto.getLabelName()==null){
            throw new ServiceException("二级标签名称不能为空");
        }
        TwoLabelDto twoLabelDto = repository.findByLabelName(dto.getOneLabel());
        if(twoLabelDto!=null){
            throw new ServiceException("二级标签名称已存在");
        }

    }

}
